package async;

import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @Author: linKe
 * @Date: 2022/1/14 17:19
 * @FileName: App
 * @version: 1.0
 * @Description: 测试类
 */
public class AppTest {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
       /* AsyncExecutor executor = new ThreadAsyncExecutor();
        System.out.println(new Date());
        AsyncResult<Integer> result1 = executor.startProcess(() -> {
            TimeUnit.SECONDS.sleep(2);

            return 1;
        });
        Integer integer = executor.endProcess(result1);
        System.out.println(result1.getValue()+""+new Date());*/
        AsyncExecutor executor = new ThreadAsyncExecutor();

        AsyncResult<Integer> result1 = executor.startProcess(() -> {
            TimeUnit.SECONDS.sleep(2);
            return 1;
        });
        System.out.println(new Date());
        System.out.println(executor.endProcess(result1));
        // 测试异步调用
        executor.startProcess(() -> {
            return "hello";
        }, (result, ex) -> {
            // 如果存在值，则返回true ，否则返回false 。
            if (ex.isPresent()) {
                System.out.println("task failed");
            } else {
                System.out.println(result + " skywalker");
            }
        });

        System.out.println(executor.endProcess(result1));
        System.out.println(new Date());
    }
}
